#include <iostream>

#include <thrift/Thrift.h>
// #include <thrift/protocol/TProtocol.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TBufferTransports.h>

#include "ArticleComment_types.h"
#include "ArticleComment_constants.h"
#include "ArticleComment.h"

using namespace std;
using namespace boost;
using namespace apache::thrift;

int main() {
  shared_ptr<transport::TTransport> transport(new transport::TMemoryBuffer(16));
  shared_ptr<protocol::TProtocol> protocol(new protocol::TBinaryProtocol(transport));

  jztzp::CommentItem item;
  item.__set_id(100);
  item.__set_state(0x02);
  item.__set_article_id(107);
  item.__set_sender_id(403201989);
  item.__set_sender_name("热爱生命");
  item.__set_time_stamp(time(NULL));
  item.__set_sender_ip("127.0.0.1");
  item.__set_content("技术是有趣的，做快乐的事情，挣钱何必那么多呢？");

  item.write(protocol.get());

  uint8_t * serialized;
  uint32_t size;
  (dynamic_cast<transport::TMemoryBuffer*>(transport.get()))->getBuffer(&serialized, &size);
  cout << "buf size : "<< size << endl;

  // std::string s = (dynamic_cast<transport::TMemoryBuffer*>(transport.get()))->getBufferAsString();
  std::string s = (dynamic_cast<transport::TMemoryBuffer*>(transport.get()))->getBufferAsString();
  cout << "buf size : "<< s.size() << endl;

  jztzp::CommentItem item2;

  item2.read(protocol.get());
  cout << item2.id << endl;
  cout << item2.sender_name << endl;

  return 0;
}
